<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/23 0023
 * Time: 13:42
 */

namespace app\common\model\mall;


use app\common\model\TimeModel;
use think\facade\Db;

class IntegralItem extends TimeModel
{
    /**
     * 关联商品规格
     */
    public function Sku()
    {
        return $this->hasMany('app\common\model\mall\IntegralItemSku', 'goods_id', 'id');
    }

    /**
     * 添加后
     */
    public static function onAfterInsert($data)
    {
        $integral_item_sku = app('app\common\model\mall\IntegralItemSku');
        $arr = [];
        $inventory = 0;
        foreach ($data->attribute as $key => $value){
            $arr[$key]['goods_id'] = $data->id;
            $arr[$key]['attribute'] = $value;
            $arr[$key]['number'] = $data['number'][$key];
            $arr[$key]['attr_price'] = $data['attr_price'][$key];
            $arr[$key]['attr_img'] = $data['attr_img'][$key];
            $arr[$key]['app_id'] = $integral_item_sku->getApp();
            $inventory += $data['number'][$key];
        }

        $article = self::find($data->id);

        $article->save(['inventory' => $inventory,'price'=>$arr[0]['attr_price']]);

        $integral_item_sku->saveAll($arr);
    }

    /**
     * 修改后
     */
    public static function onAfterUpdate($data)
    {
        $integral_item_sku = app('app\common\model\mall\IntegralItemSku');

        if (!empty($data->attr_id)) {
            $integral_item_sku->where([['id', 'notin', $data->attr_id], ['goods_id', '=', $data->id]])->delete();
            $arr = [];
            $arr1 = [];
            $inventory = 0;
            foreach ($data->attribute as $key => $value){
                $inventory += $data['number'][$key];
                if (isset($data['attr_id'][$key])){
                    $arr['goods_id'] = $data->id;
                    $arr['attribute'] = $value;
                    $arr['number'] = $data['number'][$key];
                    $arr['attr_price'] = $data['attr_price'][$key];
                    $arr['attr_img'] = $data['attr_img'][$key];
                    $arr['app_id'] = $integral_item_sku->getApp();
                    IntegralItemSku::update($arr,['id'=>$data['attr_id'][$key]]);
                } else {
                    $arr1[$key]['goods_id'] = $data->id;
                    $arr1[$key]['attribute'] = $value;
                    $arr1[$key]['number'] = $data['number'][$key];
                    $arr1[$key]['attr_price'] = $data['attr_price'][$key];
                    $arr1[$key]['attr_img'] = $data['attr_img'][$key];
                    $arr1[$key]['app_id'] = $integral_item_sku->getApp();
                }
            }
            $article = self::where('id', $data->id)->find();
            $article->save(['inventory' => $inventory]);
            if (!empty($arr1)) {
                $article->save(['price'=>$arr1[0]['attr_price']]);
                $integral_item_sku->saveAll($arr1);
            }
        }
    }

    /**
     * 删除后
     */
    public static function onAfterDelete($data)
    {
        $integral_item_sku = app('app\common\model\mall\IntegralItemSku');
        $integral_item_sku->where([['goods_id', '=', $data->id]])->delete();
    }

    public function setImageAttr($value)
    {
        return implode(',', $value);
    }

    public function getImageAttr($value)
    {
        return explode(',', $value);
    }
}